import java.util.LinkedList;
import java.util.Queue;

/**
 * @author LKQ
 * @date 2021/12/9 9:54
 * @description 利用队列实现广度优先搜索
 */
public class Solution {
    public static void main(String[] args) {

    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        Queue<Integer> queueValue = new LinkedList<>();
        queue.offer(root);
        queueValue.offer(root.val);
        while (!queue.isEmpty()) {
            TreeNode now = queue.poll();
            int temp = queueValue.poll();
            if (now.left == null && now.right == null) {
                if (temp == targetSum) {
                    return true;
                }
                continue;
            }
            if (now.left != null) {
                queue.offer(now.left);
                queueValue.offer(now.left.val + temp);
            }
            if (now.right != null) {
                queue.offer(now.right);
                queueValue.offer(now.right.val + temp);
            }
        }
        return false;
    }
}
